package com.gistone.commis.api.restController;

import com.gistone.commis.database.GetBySqlMapper;
import com.gistone.commis.dto.Notify;
import com.gistone.commis.dto.Result;
import com.gistone.commis.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.Calendar;

/**
 * Created by wangfan on 2017/6/4.
 */

@RestController("apiIndexController")
@RequestMapping("api/index")
public class IndexController {


    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);


    @Autowired
    private GetBySqlMapper getBySqlMapper;

    @RequestMapping(method = RequestMethod.GET, value = "/fundNotify")
    public Object fundNotify(HttpServletRequest request, HttpServletResponse response) {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");
        int userId = user.getUserId();
        String sql = "select * from tb_pp_change,tb_user where   tb_user.user_id =tb_pp_change.last_auditor_id " +
                "AND tb_pp_change.current_state =0 AND tb_user.parent_id =" + "'" + userId + "'";

        return
                this.getBySqlMapper.findRecords(sql);
    }

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/notify")
    public Object notify(HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        result.setStatus(0);
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");
        logger.info("用户信息：" + user.toString());
        int community_id = user.getCommunity().getCommunityId();
        int userId = user.getUserId();
        logger.info("community_id:" + community_id);
        logger.info("userTypeId:" + user.getUserTypeId());
        Notify notify = new Notify();
        try {

            String allSql = "select count(*) from tb_back_notify " +
                    " where  person_id =" + "'" + user.getUserId() + "'";

            int allBackNotify =
                    this.getBySqlMapper.findrows(allSql);
            System.out.println("allBackNotify-----"+allBackNotify);
            notify.setAllBackNotify(allBackNotify);

            if (user.getUserTypeId() == 4) {//社区用户通知
                Calendar now = Calendar.getInstance();
                int nowYear = now.get(Calendar.YEAR);
                int nowMonth = now.get(Calendar.MONTH) + 1;
                String sql = "select count(*) from tb_attendance_notify " +
                        " where  community_id =" + "'" + user.getCommunityId() + "'" +
                        " and  month =" + "'" + nowMonth + "'" +
                        " and  flag =" + "'" + 0 + "'" +
                        " and year =" + "'" + nowYear + "'";

                int attNotify =
                        this.getBySqlMapper.findrows(sql);

                notify.setAttNotify(attNotify);

                 sql = "select count(*) from tb_overtime_notify " +
                        " where  community_id =" + "'" + user.getCommunityId() + "'" +
                        " and  month =" + "'" + nowMonth + "'" +
                        " and  flag =" + "'" + 0 + "'" +
                        " and year =" + "'" + nowYear + "'";

                int overtimeNotify =
                        this.getBySqlMapper.findrows(sql);

                notify.setOvertimeNotify(overtimeNotify);


                sql = "select count(*) from tb_pp" +
                        " where  is_delete=0 and is_off =7" +
                        " and  community_id =" + "'" + user.getCommunityId() + "'";

                int fundCommunityNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + fundCommunityNotify);
                notify.setFundCommunityNotify(fundCommunityNotify);

                sql = "select count(*) from tb_office_supplies" +
                        " where  is_delete=0 and is_off =7" +
                        " and  community_id =" + "'" + user.getCommunityId() + "'";

                int officeCommunityNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + officeCommunityNotify);
                notify.setOfficeCommunityNotify(officeCommunityNotify);

                sql = "select count(*) from tb_public_fund" +
                        " where  is_delete=0 and is_off =7" +
                        " and  community_id =" + "'" + user.getCommunityId() + "'";

                int publicFundCommunityNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + publicFundCommunityNotify);
                notify.setPublicFundCommunityNotify(publicFundCommunityNotify);


                sql = "select count(*) from tb_construction" +
                        " where  is_delete=0 and is_off =7" +
                        " and  community_id =" + "'" + user.getCommunityId() + "'";

                int constructionCommunityNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + constructionCommunityNotify);
                notify.setConstructionCommunityNotify(constructionCommunityNotify);
                result.setData(notify);
                return result;
            } else if (user.getUserTypeId() == 3||user.getUserTypeId() == 6||user.getUserTypeId() == 7||user.getUserTypeId() == 8||user.getUserTypeId() == 9) {//领导 （公益金 办公经费 群众和党建经费） 通知
                String sql = "select count(*) from tb_pp_change itemC,tb_pp item, tb_user u " +
                        "where itemC.pp_id =item.pp_id and   u.user_id =itemC.current_auditor_id " +
                        "AND itemC.is_valid =0 and item.is_delete=0 and item.is_off<6  AND u.parent_id =" + "'" + userId + "'";

                int fundNotify =
                        this.getBySqlMapper.findrows(sql);
                notify.setFundNotify(fundNotify);
                sql = "select count(*) from tb_office_supplies_change " +
                        "itemC,tb_office_supplies item, tb_user u " +
                        "where itemC.supplies_id =item.supplies_id and   u.user_id =itemC.current_auditor_id " +
                        "AND itemC.is_valid =0 and item.is_delete=0 and item.is_off<6  AND u.parent_id =" + "'" + userId + "'";
                int officeSuppliesNotify =
                        this.getBySqlMapper.findrows(sql);
                logger.info("officeSuppliesNotify:" + officeSuppliesNotify);
                notify.setOfficeNotify(officeSuppliesNotify);

                sql = "select  count(*) from tb_public_fund_change pc,tb_user u,tb_public_fund p,tb_community c where   " +
                        "u.user_id =pc.current_auditor_id and pc.pf_id=p.pf_id and u.community_id = c.community_id " +
                        "and pc.is_valid =0 and u.user_type_id != 4 AND  u.parent_id =" + "'" + userId + "'";
                int publicFundNotify = this.getBySqlMapper.findrows(sql);
                logger.info("publicFundNotify:" + publicFundNotify);
                notify.setPublicFundNotify(publicFundNotify);

                sql = "select  count(*) from tb_construction_change pc,tb_user u,tb_construction p,tb_community c where   " +
                        "u.user_id =pc.current_auditor_id and pc.con_id=p.con_id and u.community_id = c.community_id " +
                        "and pc.is_valid =0 and u.user_type_id != 4  AND  u.parent_id =" + "'" + userId + "'";
                int constructionNotify = this.getBySqlMapper.findrows(sql);
                logger.info("constructionNotify:" + constructionNotify);
                notify.setConstructionNotify(constructionNotify);
                result.setData(notify);
                return result;
            } else if (user.getUserTypeId() == 2) {//科室管理员  (资产申报与删除)通知
                String sql = "select count(*) from tb_property where property_state =0 and is_delete=0";

                int addPropertyNotify =
                        this.getBySqlMapper.findrows(sql);
                logger.info("addPropertyNotify:" + addPropertyNotify);//申报资产通知

                notify.setAddProNotify(addPropertyNotify);
                sql = "select count(*) from tb_property where property_state =2 and is_delete=0";

                int delPropertyNotify =
                        this.getBySqlMapper.findrows(sql);
                logger.info("delPropertyNotify:" + delPropertyNotify);//删除资产通知


                notify.setDelProNotify(delPropertyNotify);

                Calendar now = Calendar.getInstance();
                int nowYear = now.get(Calendar.YEAR);
                int nowMonth = now.get(Calendar.MONTH) + 1;
                sql = "select count(*) from tb_attendance_notify " +
                        " where  month =" + "'" + nowMonth + "'" +
                        " and  flag =" + "'" + 2 + "'" +
                        " and year =" + "'" + nowYear + "'";

                int attNotify =
                        this.getBySqlMapper.findrows(sql);

                notify.setAttNotify(attNotify);

                sql = "select count(*) from tb_overtime_notify " +
                        " where  month =" + "'" + nowMonth + "'" +
                        " and  flag =" + "'" + 2 + "'" +
                        " and year =" + "'" + nowYear + "'";

                int overtimeNotify =
                        this.getBySqlMapper.findrows(sql);

                notify.setOvertimeNotify(overtimeNotify);

                System.out.println(notify.toString());

                result.setData(notify);
                return result;
            } else if (user.getUserTypeId() == 5) {//组织部管理员  （党建和群众经费）通知
                String sql = "select count(*) from tb_public_fund_change itemC,tb_public_fund item " +
                        "where itemC.pf_id =item.pf_id " +
                        "AND itemC.is_valid =0 and item.is_delete=0 and itemC.current_auditor_id " +
                        " in ( SELECT tb_user.user_id FROM tb_user ,tb_public_fund_change " +
                        "WHERE  tb_user.user_id=tb_public_fund_change.current_auditor_id and tb_user.user_type_id=4)";
                int publicFundNotify =
                        this.getBySqlMapper.findrows(sql);
                logger.info("publicFundNotify:" + publicFundNotify);//申报群众经费通知
                notify.setPublicFundNotify(publicFundNotify);
                sql = "select count(*) from tb_construction_change itemC,tb_construction item " +
                        "where itemC.con_id =item.con_id " +
                        "AND itemC.is_valid =0 and item.is_delete=0 and itemC.current_auditor_id " +
                        " in ( SELECT tb_user.user_id FROM tb_user ,tb_construction_change " +
                        "WHERE  tb_user.user_id=tb_construction_change.current_auditor_id and tb_user.user_type_id=4)";
                int constructionNotify =
                        this.getBySqlMapper.findrows(sql);
                logger.info("constructionNotify:" + constructionNotify);//申报党建经费通知

                notify.setConstructionNotify(constructionNotify);

                sql = "select count(*) from tb_public_fund" +
                        " where  is_delete=0 and is_off =6";

                int publicFundOrgNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + publicFundOrgNotify);
                notify.setPublicFundOrgNotify(publicFundOrgNotify);


                sql = "select count(*) from tb_construction" +
                        " where  is_delete=0 and is_off =6";

                int constructionOrgNotify =
                        this.getBySqlMapper.findrows(sql);
                System.out.println("------------" + constructionOrgNotify);
                notify.setConstructionOrgNotify(constructionOrgNotify);

                result.setData(notify);
                return result;
            }else{
                result.setData(notify);
                return result;
            }
        } catch (Exception e) {

            return new Result(1,"error","");

        }




    }

}
